package phone.qyg.com.qyg.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.app.base.tool.BaseActivity;
import com.app.config.AppHttpKey;
import com.app.config.ApplicationConfig;
import com.app.utils.JSONTool;
import com.app.utils.PromptManager;
import com.app.utils.SystemPrintl;
import com.app.view.RatingBar;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.text.MessageFormat;

import phone.qyg.com.qyg.R;
import phone.qyg.com.qyg.model.ActivityDetail;
import phone.qyg.com.qyg.model.Discuss;
import phone.qyg.com.qyg.model.DiscussItem;
import phone.qyg.com.qyg.model.VenueDetail;
import phone.qyg.com.qyg.utils.AppHttpRequest;

/**
 * 作者：lblbh on 2017/12/8 18:43
 * 邮箱：lanbuhan@163.com
 * 功能：发布讨论
 */
@ContentView(R.layout.release_discuss)
public class ReleaseDiscussActivity extends BaseActivity implements TextWatcher {

    @ViewInject(R.id.back)
    TextView back;

    @ViewInject(R.id.release)
    TextView release;

    @ViewInject(R.id.content_num)
    TextView content_num;

    @ViewInject(R.id.content)
    EditText content;

    @ViewInject(R.id.rating_rela)
    RelativeLayout rating_rela;

    @ViewInject(R.id.rating_bar)
    RatingBar rating_bar;

    int selectRatingCount;

    boolean isSecond; //二级回复使用,true-不需要评分

    boolean isRelease;

    int object_id;

    int type;

    int hintTitle;

    @Override
    public void initSucessView() {
        Object obj = getTransModels();

        if(obj instanceof VenueDetail){
            //场馆详情
            VenueDetail venueDetail = (VenueDetail) obj;
            type = 1;
            object_id = venueDetail.id;
        }else if(obj instanceof ActivityDetail){
            //活动详情
            ActivityDetail actDetail = (ActivityDetail) obj;
            type = 2;
            object_id = actDetail.id;
        }else if(obj instanceof Discuss){
            Discuss discuss = (Discuss) obj;
            isSecond = true;
            type = discuss.type;
            object_id = discuss.objectId;
            mapKeys.put(AppHttpKey.PARENT_ID,String.valueOf(discuss.id));
            mapKeys.put(AppHttpKey.REPLY_USER_ID,String.valueOf(discuss.userId));
            hintTitle = R.string.reply;
        }else if(obj instanceof DiscussItem){
            DiscussItem discussItem = (DiscussItem) obj;
            isSecond = true;
            type = discussItem.type;
            object_id = discussItem.objectId;
            mapKeys.put(AppHttpKey.PARENT_ID,String.valueOf(discussItem.id));
            mapKeys.put(AppHttpKey.REPLY_USER_ID,String.valueOf(discussItem.userId));
            hintTitle = R.string.reply;
        }
    }

    @Event(value = {R.id.back,R.id.release})
    private void viewOnClick(View v){
        switch (v.getId()){
            case R.id.back:
                this.finish();
                break;
            case R.id.release:
                if(!isRelease){
                    PromptManager.showToast(R.string.plerase_edit_discuss_content);
                    return;
                }

                if(type ==1 && selectRatingCount <=0 && !isSecond){
                    PromptManager.showToast(R.string.plerase_score);
                    return;
                }

                release.setEnabled(false);
                //进行网络请求
                setReleaseDiscuss();
                break;
        }
    }

    private void setReleaseDiscuss(){
        /**
         “objectId”: 11   //评论对象ID（如：活动、场馆、课程）（必填）
         “type”:1 //评论类型：1-场馆（必填）2活动 3课程
         “content”:”我觉得不错...”  //评论内容（必填）
         */
        String contentStr = getEditToString(content);
        mapKeys.put(AppHttpKey.OBJECT_ID,String.valueOf(object_id));
        mapKeys.put(AppHttpKey.TYPE,String.valueOf(type));
        mapKeys.put(AppHttpKey.CONTENT,contentStr);
        mapKeys.put(AppHttpKey.GRADE,String.valueOf(selectRatingCount));

        AppHttpRequest.appQuitState(ApplicationConfig.PUB_COMMENT,
                mapKeys, new AppHttpRequest.OnAppHttpListener() {
                    @Override
                    public void onAppHttpState(boolean isState, String result) {
                        if(isState){
                            String hint = JSONTool.errorHint(result);
                            PromptManager.showToast(hint);
                            ReleaseDiscussActivity.this.finish();
                        }
                    }
                });
    }

    @Override
    public void initViewHandler() {
        content.addTextChangedListener(this);
        rating_bar.setStepSize(RatingBar.StepSize.Full);//设置每次点击增加一颗星还是半颗星
        rating_bar.setOnRatingChangeListener(new RatingBar.OnRatingChangeListener() {
            @Override
            public void onRatingChange(float ratingCount) {//点击星星变化后选中的个数
                SystemPrintl.systemPrintl("RatingBar-Count="+ratingCount);
                selectRatingCount = ((int)ratingCount * 2);
            }
        });
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        isRelease = false;
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        String contentStr = getEditToString(content);

        if(isRequestStr(contentStr) && !content.equals(getString(R.string.release_dynamic_experience))){
            String hintStr = MessageFormat.format(getString(R.string.release_content_num),contentStr.length());
            content_num.setText(hintStr);
            isRelease = true;
        }else{
            isRelease = false;
        }
    }

    @Override
    public void afterTextChanged(Editable s) {}

    @Override
    public void initData(Bundle savedInstanceState) {
        rating_rela.setVisibility((type ==1 && !isSecond)?View.VISIBLE:View.GONE);
        back.setText((type ==1)?R.string.release_discuss_two:R.string.release_discuss);

        if(hintTitle >0){
            back.setText(hintTitle);
        }
    }
}
